package myapps.ttclient.activity.connection;

import myapps.ttclient.app.TTClientApplication;
import myapps.ttclient.connection.ConnectionListManager;
import myapps.ttclient.connection.Connection_Bluetooth;
import myapps.ttclient.R;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ConnectionEditActivity_Bluetooth extends ConnectionEditActivity 
		implements OnClickListener {
	
	private static final int ADDRESS_REQUEST_CODE = 0;
	
	private TTClientApplication application;
	private ConnectionListManager connectionListManager;
	
	private Connection_Bluetooth connection;
	
	private EditText address;
	private Button edit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.application = (TTClientApplication) this.getApplication();

		this.connectionListManager = this.application.getConnectionListManager();
		
		this.setContentView(R.layout.connectionbluetoothedit);
		
		super.onCreate(savedInstanceState);
		
		this.connection = (Connection_Bluetooth) connectionParam;
		
		this.address = (EditText) this.findViewById(R.id.address);
		
		this.edit = (Button) this.findViewById(R.id.edit);
		this.edit.setOnClickListener(this);
		
		getCommonIntentContent();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		
		if (v == this.edit) {
			this.startActivityForResult(new Intent(this, BluetoothDevicesActivity.class), 
					ADDRESS_REQUEST_CODE);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode == RESULT_OK) {
			if (requestCode == ADDRESS_REQUEST_CODE) {
				this.address.setText(data.getStringExtra("address"));
			}
		}
	}
	
	@Override
	public void onSave() {
		this.connection.setAddress(this.address.getText().toString());
		
		super.onSave();
		
		if(type == TYPE_NEW) {
			this.connectionListManager.getConnectionList_Bluetooth().add(this.connection);
		}
	}
}
